package com.smart.context;

import com.smart.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author chenxingyu
 * @date 2019/8/27 11:14
 */
public class ContextBeanLifeCycle {
    private static void lifeCycleInBeanFactory(){
        //装载配置文件并启动容器
        ApplicationContext ctx = new AnnotationConfigApplicationContext(Beans.class);
        //第一次从容器中获取car,将触发容器实例化该bean,将引发Bean生命周期方法的调用
        Car car1 = (Car) ctx.getBean("car");
        System.out.println(car1);
        car1.setColor("红色");

        //第二次从容器中获取car,直接从缓存池中获取
        Car car2 = (Car) ctx.getBean("car");

        //查看car1和car2是否指向同一引用
        System.out.println("car1==car2:"+(car1==car2));
        System.out.println(car2);

        //关闭容器

    }

    public static void main(String[] args) {
        lifeCycleInBeanFactory();
    }
}
